# prepare ingredients for plot
V(g)$size = 10
V(g)$label = V(g)$name
V(g)$degree = degree(g)
#V(g)$label.cex = 1.5 * log10(V(g)$degree)
V(g)$label.color = hsv(0, 0, 0.2, 0.55)
V(g)$frame.color = NA
V(g)$color = gcols
E(g)$color = hsv(0, 0, 0.7, 0.3)
# plot
plot(g, layout=glay)
title("\nGraph of tweets about genetics and genomics",
col.main="gray40", cex.main=1.5, family="serif")
plot(g)
MiTerMat2 <- removeSparseTerms(MiTerMat, sparse=0.95)
# cluster terms
distMatrix <- dist(scale(as.matrix(MiTerMat2)))
fit <- hclust(distMatrix, method="ward")
plot(fit)
rect.hclust(fit, k=5, border="red")
groups <- cutree(fit, k=5)
groups
cutree(fit, k=5)
MiTerMat <- TermDocumentMatrix(MiCorpus,
control=list(wordLengths=c(1,15))
)
MiTerMat
### ----------------
### Popular Words
### ----------------
# inspect most popular words
findFreqTerms(MiTerMat, lowfreq=50)
# Calculate Frequency
m <- as.matrix(MiTerMat)
wordFreq <- sort(rowSums(m), decreasing=TRUE)
# Barplot Top 20
barplot(head(wordFreq, 20), border=NA, las=2, main="Top 20", cex.main=1,)
# Wordcloud
library(wordcloud)
library(RColorBrewer)
set.seed(12345)
wordcloud(words=names(wordFreq),
freq=wordFreq,
min.freq=30,
random.order=FALSE,
colors=brewer.pal(8, "Dark2"))
### ----------------
### Words associated with a term
### ----------------
findAssocs(MiTerMat, "privatizar", 0.20)
### ----------------
### Clustering Words
### ----------------
# remove sparse terms to simplify the cluster plot
# Note: tweak the sparse parameter to determine the number of words.
# About 10-30 words is good.
MiTerMat2 <- removeSparseTerms(MiTerMat, sparse=0.95)
# cluster terms
distMatrix <- dist(scale(as.matrix(MiTerMat2)))
fit <- hclust(distMatrix, method="ward")
plot(fit)
# cut tree into 5 clusters
cutree(fit, k=5)
rect.hclust(fit, k=5, border="red")
# convert the sparse term-document matrix to a standard data frame
mydata.df <- as.data.frame(inspect(mydata.dtm2))
# inspect dimensions of the data frame
nrow(mydata.df)
ncol(mydata.df)
mydata.df.scale <- scale(mydata.df)
d <- dist(mydata.df.scale, method = "euclidean") # distance matrix
fit <- hclust(d, method="ward")
plot(fit) # display dendogram?
groups <- cutree(fit, k=5) # cut tree into 5 clusters
# draw dendogram with red borders around the 5 clusters
rect.hclust(fit, k=5, border="red")
MiTerMat <- TermDocumentMatrix(MiCorpus,
control=list(wordLengths=c(1,15))
)
MiTerMat
### ----------------
### Popular Words
### ----------------
# inspect most popular words
findFreqTerms(MiTerMat, lowfreq=50)
# Calculate Frequency
m <- as.matrix(MiTerMat)
wordFreq <- sort(rowSums(m), decreasing=TRUE)
# Barplot Top 20
barplot(head(wordFreq, 20), border=NA, las=2, main="Top 20", cex.main=1,)
# Wordcloud
library(wordcloud)
library(RColorBrewer)
set.seed(12345)
wordcloud(words=names(wordFreq),
freq=wordFreq,
min.freq=30,
random.order=FALSE,
colors=brewer.pal(8, "Dark2"))
### ----------------
### Words associated with a term
### ----------------
findAssocs(MiTerMat, "privatizar", 0.20)
### ----------------
### Clustering Words
### ----------------
# remove sparse terms to simplify the cluster plot
# Note: tweak the sparse parameter to determine the number of words.
# About 10-30 words is good.
MiTerMat2 <- removeSparseTerms(MiTerMat, sparse=0.95)
# cluster terms
distMatrix <- dist(scale(as.matrix(MiTerMat2)))
fit <- hclust(distMatrix, method="ward")
plot(fit)
# cut tree into 5 clusters
cutree(fit, k=5)
rect.hclust(fit, k=5, border="red")
# convert the sparse term-document matrix to a standard data frame
mydata.df <- as.data.frame(inspect(mydata.dtm2))
# inspect dimensions of the data frame
nrow(mydata.df)
ncol(mydata.df)
mydata.df.scale <- scale(mydata.df)
d <- dist(mydata.df.scale, method = "euclidean") # distance matrix
fit <- hclust(d, method="ward")
plot(fit) # display dendogram?
groups <- cutree(fit, k=5) # cut tree into 5 clusters
# draw dendogram with red borders around the 5 clusters
rect.hclust(fit, k=5, border="red")
MiTerMat
findFreqTerms(MiTerMat, lowfreq=50)
rm(list = ls(all = TRUE)) #limpiar workspace
### ----------------
### Oath
### ----------------
require(ROAuth)
#requestURL <- "https://api.twitter.com/oauth/request_token"
#accessURL <- "https://api.twitter.com/oauth/access_token"
#authURL <- "https://api.twitter.com/oauth/authorize"
#cred <- OAuthFactory$new(consumerKey="1yoCAcVXfSKdUtveoyhA",
#consumerSecret="npZf58oHABtCQqoIzZ8hOfHT7GJ2nPZbXWFdKZKZ94",
#requestURL=requestURL,
#accessURL=accessURL,
#authURL=authURL)
#cred$handshake()
#https://api.twitter.com/oauth/authorize?oauth_token=mo61xXGLQi28iJe5jhAaMjasRJ6GzxwG8AeLOYOqLZQ
#save(cred, file="cred")
load("~/credentials_twitteR")
registerTwitterOAuth(cred)
### ----------------
### Search
### ----------------
require(twitteR)
# Tema
tweets <- searchTwitter("reformaenergetica", n=1000)
### ----------------
### Create Corpus
### ----------------
require(tm)
df <- do.call("rbind", lapply(tweets, as.data.frame))
MiCorpus <- Corpus(VectorSource(df$text))
# Tolower
MiCorpus <- tm_map(MiCorpus, tolower)
# Punctuacion
MiCorpus <- tm_map(MiCorpus, removePunctuation)
# Numbers
MiCorpus <- tm_map(MiCorpus, removeNumbers)
# URLs
removeURL <- function(x) gsub("http[[:alnum:]]*","",x)
MiCorpus <- tm_map(MiCorpus, removeURL)
# Retweet entities
MiCorpus <- tm_map(MiCorpus, gsub, pattern="(RT|via)((?:\\b\\W*@\\w+)+)", replacement="")
# At people
MiCorpus <- tm_map(MiCorpus, gsub, pattern="@\\w+", replacement="")
# Stopwords
MiStopWords <- stopwords("spanish")
MiCorpus <- tm_map(MiCorpus, removeWords, MiStopWords)
### ----------------
### Stemming Words
### ----------------
# Keep a copy for dictionary
#MiCorpusCopy <- MiCorpus
# Stem words
#require(Snowball)
#require(RWeka)
#require(RJava)
#require(RWekajars)
### ----------------
### Term Matrix
### ----------------
MiTerMat <- TermDocumentMatrix(MiCorpus)
MiTerMat
### ----------------
### Popular Words
### ----------------
# inspect most popular words
findFreqTerms(MiTerMat, lowfreq=50)
# Calculate Frequency
m <- as.matrix(MiTerMat)
wordFreq <- sort(rowSums(m), decreasing=TRUE)
# Barplot Top 20
barplot(head(wordFreq, 20), border=NA, las=2, main="Top 20", cex.main=1,)
# Wordcloud
library(wordcloud)
library(RColorBrewer)
set.seed(12345)
wordcloud(words=names(wordFreq),
freq=wordFreq,
min.freq=30,
random.order=FALSE,
colors=brewer.pal(8, "Dark2"))
?searchTwitter
rm(list = ls(all = TRUE)) #limpiar workspace
### ----------------
### Oath
### ----------------
require(ROAuth)
#requestURL <- "https://api.twitter.com/oauth/request_token"
#accessURL <- "https://api.twitter.com/oauth/access_token"
#authURL <- "https://api.twitter.com/oauth/authorize"
#cred <- OAuthFactory$new(consumerKey="1yoCAcVXfSKdUtveoyhA",
#consumerSecret="npZf58oHABtCQqoIzZ8hOfHT7GJ2nPZbXWFdKZKZ94",
#requestURL=requestURL,
#accessURL=accessURL,
#authURL=authURL)
#cred$handshake()
#https://api.twitter.com/oauth/authorize?oauth_token=mo61xXGLQi28iJe5jhAaMjasRJ6GzxwG8AeLOYOqLZQ
#save(cred, file="cred")
load("~/credentials_twitteR")
registerTwitterOAuth(cred)
### ----------------
### Search
### ----------------
require(twitteR)
# Tema
tweets <- searchTwitter("reformaenergetica", n=10000,
since="2012-03-01",
until="2012-03-18")
rm(list = ls(all = TRUE)) #limpiar workspace
### ----------------
### Oath
### ----------------
require(ROAuth)
require(twitteR)
#requestURL <- "https://api.twitter.com/oauth/request_token"
#accessURL <- "https://api.twitter.com/oauth/access_token"
#authURL <- "https://api.twitter.com/oauth/authorize"
#cred <- OAuthFactory$new(consumerKey="1yoCAcVXfSKdUtveoyhA",
#consumerSecret="npZf58oHABtCQqoIzZ8hOfHT7GJ2nPZbXWFdKZKZ94",
#requestURL=requestURL,
#accessURL=accessURL,
#authURL=authURL)
#cred$handshake()
#https://api.twitter.com/oauth/authorize?oauth_token=mo61xXGLQi28iJe5jhAaMjasRJ6GzxwG8AeLOYOqLZQ
#save(cred, file="cred")
load("~/credentials_twitteR")
registerTwitterOAuth(cred)
### ----------------
### Search
### ----------------
require(twitteR)
# Tema
tweets <- searchTwitter("reformaenergetica", n=10000,
since="2012-03-01",
until="2012-03-18")
require(twitteR)
# Tema
tweets <- searchTwitter("reformaenergetica", n=5000,
since="2012-03-01",
until="2012-03-18")
install.packages("~/Desktop/BARD_1.24.tar.gz", repos = NULL, type = "source")
?install.packages
install.packages("~/Desktop/BARD_1.24.tar.gz", repos = NULL, type = "source", dependencies=TRUE)
library(BARD)
install.packages("spdep")
install.packages("~/Desktop/BARD_1.24.tar.gz", repos = NULL, type = "source")
library(BARD)
1000*.5
38+49
.05^2
.03^2
((.53/(.05^2)) + (.6/(.03^2))) / ((1/(.05^2)) + (1/(.03^2)))
((1/(.05^2)) + (1/(.03^2)))^(-1)
sqrt((1/(.05^2)) + (1/(.03^2)))^(-1)
sqrt((1/(.05^2)) + (1/(.03^2)))^-1
sqrt(((1/(.05^2)) + (1/(.03^2)))^-1)
.025*2
((.53/(.05^2)) + (.6/(.05^2))) / ((1/(.05^2)) + (1/(.05^2)))
sqrt(((1/(.05^2)) + (1/(.05^2)))^-1)
.58 - (2*.026)
.58 - (3*.026)
.58 - (2*.035)
install.packages("flexmix")
?flexmix
library(flexmix)
?flexmix
install.packages("mclust")
library(mclust)
?mclust
??mclust
library(seatsvotes)
mvnmix
?lmer
install.packages("LMERConvenienceFunctions")
?lmer
install.packages("lmerTest")
library(lmer)
library(XML)
url<- "http://es.wikipedia.org/wiki/Anexo:Municipios_de_la_Cruzada_contra_el_Hambre"
cruzada <- readHTMLTable(url, skip.rows=1:2)
cruzada <- as.data.frame(cruzada)
cruzada <- rbind(as.matrix(cruzada[,1:3]),as.matrix(cruzada[,4:6]))
cruzada <- data.frame(ID=cruzada[,1], cruzada=rep(1, nrow(cruzada)))
View(cruzada)
# Fatalities per 100,000 Population
fatality.rate <- c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64)
names(fatality.rate) <- seq(2011:1994)
fix(fatality.rate)
seq(2011:1994)
seq(1994:2011)
?seq
2011:1994
# Fatalities per 100,000 Population
# http://www-fars.nhtsa.dot.gov/Main/index.aspx
fatality.rate <- c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64)
names(fatality.rate) <- 2011:1994
fatality.rate
fatality.rate <- c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64)
names(fatality.rate) <- 2011:1994
fatality.rate
fatality.rate <- rev(c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64))
names(fatality.rate) <- 1994:2011
fatality.rate
# Fatalities per 100,000 Population
# http://www-fars.nhtsa.dot.gov/Main/index.aspx
fatality.rate <- rev(c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64))
names(fatality.rate) <- 1994:2011
lemons <- c(303.4,224.3,233.0,285.1,362.6,436.7,528.5,600.7)
cbind(fatality.rate, lemons)
?cbind
# Fatalities per 100,000 Population
# http://www-fars.nhtsa.dot.gov/Main/index.aspx
fatality.rate <- rev(c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64))
names(fatality.rate) <- 1994:2011
lemons <- c(303.4,224.3,233.0,285.1,362.6,436.7,528.5,600.7)
names(lemons) <- 1994:2001
cbind(fatality.rate[1:length(lemons)], lemons)
plot(fatality.rate, lemons)
data <- cbind(fatality.rate[1:length(lemons)], lemons)
plot(data)
data <- cbind(lemons, fatality.rate[1:length(lemons)], )
plot(data)
data <- cbind(lemons, fatality.rate[1:length(lemons)])
plot(data)
data
# Fatalities per 100,000 Population
# http://www-fars.nhtsa.dot.gov/Main/index.aspx
fatality.rate <- rev(c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64))
names(fatality.rate) <- 1994:2011
# http://www.fas.usda.gov/gats/default.aspx
# 0805302000
lemons <- c(303.4,224.3,233.0,285.1,362.6,436.7,528.5,600.7,1006.9,2746.4,12703.8,12501.6,15690.8,40402.1,17951.6,24269.5,25742.8,27210.1)
names(lemons) <- 1994:2011
data <- cbind(lemons, fatality.rate)
plot(data)
?text
data
names(data)
rownames(data)
plot(data)
text(data, labels=rownames(data))
plot(data)
text(data, labels=rownames(data), pos=4, cex=.5)
?lm
lm(fatality.rate ~ lemons, data=data)
lm(fatality.rate ~ lemons, data=as.data.frame(data))
library(arm)
display(lm(fatality.rate ~ lemons, data=as.data.frame(data)))
plot(fatality.rate[1:length(lemons.94.01)], lemons.94.01)
fatality.rate[1:length(lemons.94.01)]
# Fatalities per 100,000 Population
# http://www-fars.nhtsa.dot.gov/Main/index.aspx
fatality.rate <- rev(c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64))
names(fatality.rate) <- 1994:2011
# http://www.fas.usda.gov/gats/default.aspx
# 0805302000 LEMONS, FRESH
lemons.94.01 <- c(303.4,224.3,233.0,285.1,362.6,436.7,528.5,600.7)
names(lemons.94.01) <- 1994:2001
# 0805502000 LEMONS, FRESH/DR
lemons.02.11 <- c(1006.9,2746.4,12703.8,12501.6,15690.8,40402.1,17951.6,24269.5,25742.8,27210.1)
names(lemons.02.11) <- 2002:2011
plot(fatality.rate[1:length(lemons.94.01)], lemons.94.01)
fatality.rate[names(fatality.rate) %in% 1994:2001]
# Fatalities per 100,000 Population
# http://www-fars.nhtsa.dot.gov/Main/index.aspx
fatality.rate <- rev(c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64))
names(fatality.rate) <- 1994:2011
# http://www.fas.usda.gov/gats/default.aspx
# 0805302000 LEMONS, FRESH
lemons.94.01 <- c(303.4,224.3,233.0,285.1,362.6,436.7,528.5,600.7)
names(lemons.94.01) <- 1994:2001
# 0805502000 LEMONS, FRESH/DR
lemons.02.11 <- c(1006.9,2746.4,12703.8,12501.6,15690.8,40402.1,17951.6,24269.5,25742.8,27210.1)
names(lemons.02.11) <- 2002:2011
data1 <- cbind(fatality.rate[names(fatality.rate) %in% 1994:2001],
lemons.94.01)
data2 <- cbind(fatality.rate[names(fatality.rate) %in% 2002:2011],
lemons.02.11)
plot(data1)
# Fatalities per 100,000 Population
# http://www-fars.nhtsa.dot.gov/Main/index.aspx
fatality.rate <- rev(c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64))
names(fatality.rate) <- 1994:2011
# http://www.fas.usda.gov/gats/default.aspx
# 0805302000 LEMONS, FRESH
lemons.94.01 <- c(303.4,224.3,233.0,285.1,362.6,436.7,528.5,600.7)
names(lemons.94.01) <- 1994:2001
# 0805502000 LEMONS, FRESH/DR
lemons.02.11 <- c(1006.9,2746.4,12703.8,12501.6,15690.8,40402.1,17951.6,24269.5,25742.8,27210.1)
names(lemons.02.11) <- 2002:2011
data1 <- cbind(lemons.94.01,
fatality.rate[names(fatality.rate) %in% 1994:2001])
data2 <- cbind(lemons.02.11,
fatality.rate[names(fatality.rate) %in% 2002:2011])
plot(data1)
text(data1, labels=rownames(data1), pos=4, cex=.5)
plot(data2)
text(data2, labels=rownames(data2), pos=4, cex=.5)
data1
library(maps)
?smooth.maps
?smooth.map
data(state, package = "datasets")
data(votes.repub)
z = votes.repub[, "1900"]
m = map("state", fill = TRUE, plot = FALSE)
fit = smooth.map(m, z, span = 1/100, merge = TRUE, ave = TRUE)
mat = tapply(fit$z, fit[1:2], mean)
gray.colors <- function(n) gray(rev(0:(n - 1))/n)
par(bg = "blue")
filled.contour(mat, color.palette = gray.colors, nlev = 32, asp = 1)
# another way to visualize:
image(mat, col = gray.colors(100))
fit = smooth.map(m, z, span = 1/100, merge = TRUE, ave = TRUE)
mat = tapply(fit$z, fit[1:2], mean)
gray.colors <- function(n) gray(rev(0:(n - 1))/n)
par(bg = "blue")
filled.contour(mat, color.palette = gray.colors, nlev = 32, asp = 1)
require(graphics); require(grDevices)
x  <- as.matrix(mtcars)
rc <- rainbow(nrow(x), start = 0, end = .3)
cc <- rainbow(ncol(x), start = 0, end = .3)
hv <- heatmap(x, col = cm.colors(256), scale = "column",
RowSideColors = rc, ColSideColors = cc, margins = c(5,10),
xlab = "specification variables", ylab =  "Car Models",
main = "heatmap(<Mtcars data>, ..., scale = \"column\")")
utils::str(hv) # the two re-ordering index vectors
71/300
rm(list = ls(all = TRUE)) #clear workspace
setwd("~/REDISTRICTING/ALTERNATIVAS_APORCIONAMIENTO")
# Read files
con.est <- read.table("Censo_2010_con_estimacion.csv",
sep=";", row.names=1, skip=4, header=TRUE)[2:33,]
sin.est <- read.table("Censo_2010_sin_estimacion.csv",
sep=";", row.names=1, skip=4, header=TRUE)[2:33,]
ife <- read.table("IFE_NACIONAL_2012.txt",sep=";", row.names=2, header=TRUE)
# Check
cbind(row.names(con.est),row.names(sin.est),rownames(ife))
cbind(colnames(con.est),colnames(sin.est))
colnames(ife)
# Data
abbrev <- c('Ags','BC','BCS','Cam','Coah', 'Col', 'Chis',
'Chih','DF','Dgo','Gto','Gro','Hgo','Jal',
'Méx','Mich','Mor','Nay','NL','Oax','Pue',
'Qro','QR','SLP','Sin','Son','Tab','Tamps',
'Tlax','Ver','Yuc','Zac')
data <- data.frame(Total.con.est=con.est$Total,
Total.sin.est=sin.est$Total,
Mas.18.con.est=rowSums(con.est[,9:24]),
Mas.18.sin.est=rowSums(sin.est[,9:24]),
Lista.nominal=ife$LISTA.NOMINAL,
row.names=abbrev)
data
## Función que asigna
resto.mayor <- function(x, n=300, min=2) {
# Media nacional por distrito
media <- sum(x)/n
# Distritos enteros
enteros <- floor(x/media)
# Asignacion directa de distritos
directa <- rep(0,length(x))
directa[enteros < min] <- min-enteros[enteros < min]
# Asignacion por resto mayor
por.asignar <- n-sum(enteros+directa)
modulo <- x %% media
modulo[directa > 0] <- 0 # con asignacion directa
resto <- rep(0,length(x))
resto[sort(modulo, decreasing=TRUE, index.return=TRUE)$ix[1:por.asignar]] <- 1
# Return
return(cbind(enteros,directa,resto, total=enteros+directa+resto))
}
## Asignación
out <- lapply(data, resto.mayor)
out
## Resultado
dist.2005 <- c(3, 8, 2, 2, 7, 2, 12, 9, 27, 4, 14, 9, 7, 19, 40, 12,
5, 3, 12, 11, 16, 4, 3, 7, 8, 7, 6, 8, 3, 21, 5, 4)
table <- data.frame(Distritos.2005=dist.2005,
sapply(out, function(x) x[,"total"]),
row.names=abbrev)
table
38+49
